<template>
  <div ref="dom" class="charts chart-line"></div>
</template>

<script>
import echarts from 'echarts'
import tdTheme from './theme.json'
import { on, off } from '@/libs/tools'
echarts.registerTheme('tdTheme', tdTheme)
export default {
  name: 'ChartLine',
  props: {
    barObj: Object,
    text: String,
    subtext: String
  },
  watch:{
    barObj:{
      handler(newValue, oldValue) {
          this.value = newValue;
          this.renderData()
　　　　},
　　　　deep: true
    }
  },
  data () {
    return {
      dom: null
    }
  },
  methods: {
    resize () {
      this.dom.resize()
    },
    renderData(){
      this.$nextTick(() => {
        let option = {
          title: {
            text: this.text,
            subtext: this.subtext,
            x: 'center'
          },
          grid: {  
              left: '1%',  
              right: '1%',  
              bottom: '1%',  
              containLabel: true  
          },
          xAxis: {
            type: 'category',
            data: this.barObj.xAxisData
            // data: ["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"]
          },
          yAxis: [{
            name: '冷量(Kcal)',
            type: 'value'
          },{
            name: '产量(辆)',
            type: 'value'
          }],
          legend: {
            data:['冷量','产量'],
            x: 'right'
          },
          tooltip: {
            trigger: 'axis'
          },
          series: [{
            // data: seriesData,
            name: '冷量',
            symbol: 'none',
            data: this.barObj.carbonData,
            // data: [2185,1530,1772,1903,1231,1488,1551,2013,1942,2170,2190,1757],
            type: 'line',
            yAxisIndex:0,
          },{
            // data: seriesData,
            name: '产量',
            symbol: 'none',
            data: this.barObj.yieldData,
            // data: [1490,1814,1483,1313,1977,2221,2121,1837,2087,2144,1850,2108],
            type: 'line',
            yAxisIndex:1,
          }]
        }
        this.dom = echarts.init(this.$refs.dom, 'tdTheme')
        this.dom.showLoading({
            text : '正在加载数据'
        });
        this.dom.setOption(option)
        this.dom.hideLoading()
        on(window, 'resize', this.resize)
      })
    }
  },
  mounted () {
    this.$nextTick(() => {
      let option = {
        title: {
          text: this.text,
          subtext: this.subtext,
          x: 'center'
        },
        grid: {  
            left: '1%',  
            right: '1%',  
            bottom: '1%',  
            containLabel: true  
        },
        xAxis: {
          type: 'category',
          data: this.barObj.xAxisData
          // data: ["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"]
        },
        yAxis: [{
          name: '冷量(Kcal)',
          type: 'value'
        },{
          name: '产量(辆)',
          type: 'value'
        }],
        legend: {
          data:['冷量','产量'],
          x: 'right'
        },
        tooltip: {
          trigger: 'axis'
        },
        series: [{
          // data: seriesData,
          name: '冷量',
          symbol: 'none',
          data: this.barObj.carbonData,
          // data: [2185,1530,1772,1903,1231,1488,1551,2013,1942,2170,2190,1757],
          type: 'line',
          yAxisIndex:0,
        },{
          // data: seriesData,
          name: '产量',
          symbol: 'none',
          data: this.barObj.yieldData,
          // data: [1490,1814,1483,1313,1977,2221,2121,1837,2087,2144,1850,2108],
          type: 'line',
          yAxisIndex:1,
        }]
      }
      this.dom = echarts.init(this.$refs.dom, 'tdTheme')
      this.dom.showLoading({
          text : '正在加载数据'
      });
      this.dom.setOption(option)
      this.dom.hideLoading()
      on(window, 'resize', this.resize)
    })
  },
  beforeDestroy () {
    off(window, 'resize', this.resize)
  }
}
</script>
